//
//  ImagesShowViewController.m
//  Hipay_Business
//
//  Created by Mac on 16/7/19.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "ImagesShowViewController.h"
#import <UIImageView+AFNetworking.h>
#import "Constants.h"

@interface ImagesShowViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong) NSMutableArray *allImages;
@property(nonatomic,strong) UIScrollView *mainView;
@end

@implementation ImagesShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)initView{
    [super initView];
    [self setTitle:self.name];
    if (self.canDel) {
        [self.navBar createRightBtn:@"删除" block:^{
            NSInteger index = _mainView.contentOffset.x / WindowWidth;
            NSMutableArray *imgs = [NSMutableArray arrayWithArray:self.images];
            [imgs removeObjectAtIndex:index];
            
            for (UIScrollView *view in _mainView.subviews) {
                if (view.frame.origin.x / WindowWidth == index) {
                    [view removeFromSuperview];
                }else if(view.frame.origin.x / WindowWidth > index){
                    CGRect frame = view.frame;
                    frame.origin.x -= WindowWidth;
                    view.frame = frame;
                }
            }
            [_mainView setContentSize:CGSizeMake(imgs.count *WindowWidth, WindowHeight - self.navBar.frame.size.height)];
            if (self.finishDelBlock) {
                self.finishDelBlock(index);
            }
            self.images = imgs;
//            if (self.images.count == 0) {
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                
            }];
           
//            }
        }];
    }
    
    _mainView = [[UIScrollView alloc] init];
    [self.view addSubview:_mainView];
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.and.bottom.equalTo(self.view);
        make.top.equalTo(self.navBar.mas_bottom);
    }];
    [_mainView setBackgroundColor:[UIColor blackColor]];
    [_mainView setPagingEnabled:YES];
    
    if (self.images.count == 0) {
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            
        }];
        return;
    }
    _allImages = [NSMutableArray arrayWithArray:self.images];
    int i = 0;
    for (id img in self.images) {
        
        UIScrollView *scrollview = [[UIScrollView alloc] init];
        [_mainView addSubview:scrollview];
        [scrollview setFrame:CGRectMake(i * WindowWidth, 0, WindowWidth, WindowHeight - self.navBar.frame.size.height)];
        [scrollview setDelegate:self];
        scrollview.maximumZoomScale = 2.0;
        scrollview.minimumZoomScale = 1.0;
//        scrollview.contentSize = CGSizeMake(MIN(img.size.width, WindowWidth), img.size.height * (WindowHeight / MIN(img.size.height, WindowHeight));
        
        UIView *bgView = [[UIView alloc] init];
        [scrollview addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(scrollview);
            make.top.equalTo(scrollview);
            make.height.equalTo(@(WindowHeight - self.navBar.frame.size.height));
            make.width.equalTo(@(WindowWidth));
        }];
        UIImageView *imageView = [[UIImageView alloc] init];
        [bgView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(bgView);
        }];
//        UIImage *imgtemp = [CClass resizeImage:img ByWidth:WindowWidth];
//        scrollview.contentSize = imgtemp.size;
//        [imageView setImage:imgtemp];
        if ([img isKindOfClass:[NSString class]]) {
            __block typeof(imageView) tempview = imageView;
            [imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[Constants imgWithKey:img]]] placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
                UIImage *img = [CClass resizeImage:image ByWidth:WindowWidth];
                scrollview.contentSize = img.size;
                [tempview setImage:img];
            } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
                
            }];
            i ++;
        }else if ([img isKindOfClass:[UIImage class]])
        {
            __block typeof(imageView) tempview = imageView;
            UIImage *img1 = [CClass resizeImage:img ByWidth:WindowWidth];
            scrollview.contentSize = img1.size;
            [tempview setImage:img1];
            i ++;
        }

        }
          [_mainView setContentSize:CGSizeMake(WindowWidth * self.images.count, WindowHeight - self.navBar.frame.size.height)];
    [_mainView setContentOffset:CGPointMake(self.showindex * WindowWidth, 0)];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [scrollView.subviews firstObject];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
